package org.gdstash.db;

import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class DBClassTableButton {
    private static final String TABLE_NAME = "GDC_CLASSTABLE_BUTTON";
    private static final int ROW_CLASSTABLE_ID = 1;
    private static final int ROW_BUTTON_ID = 2;
    private static final int ROW_INDEX = 3;
    private String classTableID;
    private String buttonID;
    private int index;
    private DBSkillButton dbButton;
    private DBSkill dbSkill;

    public DBClassTableButton() {
        this.classTableID = null;
        this.buttonID = null;
        this.index = -1;
    }

    public DBClassTableButton(final String classTableID, final String buttonID, final int index) {
        this.classTableID = classTableID;
        this.buttonID = buttonID;
        this.index = index;
    }

    public String getButtonID() {
        return this.buttonID;
    }

    public int getIndex() {
        return this.index;
    }

    public DBSkillButton getButton() {
        return this.dbButton;
    }

    public DBSkill getSkill() {
        return this.dbSkill;
    }

    public static void createTable(final Connection conn) throws SQLException {
        final String dropTable = "DROP TABLE GDC_CLASSTABLE_BUTTON";
        final String createTable = "CREATE TABLE GDC_CLASSTABLE_BUTTON (CLASSTABLE_ID      VARCHAR(256) NOT NULL, BUTTON_ID          VARCHAR(256), INDEX              INTEGER, PRIMARY KEY (CLASSTABLE_ID, BUTTON_ID))";
        try (final Statement st = conn.createStatement()) {
            if (GDDBUtil.tableExists(conn, "GDC_CLASSTABLE_BUTTON")) {
                st.execute(dropTable);
            }
            st.execute(createTable);
            st.close();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            final Object[] args = {"GDC_CLASSTABLE_BUTTON"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_CREATE_TABLE", args);
            GDMsgLogger.addError(msg);
            throw ex;
        }
    }

    public static void insertButtons(final Connection conn, final String id, final List<DBClassTableButton> buttons) throws SQLException {
        final String insert = "INSERT INTO GDC_CLASSTABLE_BUTTON VALUES (?,?,?)";
        if (buttons == null) {
            return;
        }
        if (buttons.isEmpty()) {
            return;
        }
        try (final PreparedStatement ps = conn.prepareStatement(insert)) {
            for (final DBClassTableButton button : buttons) {
                ps.setString(1, id);
                ps.setString(2, button.buttonID);
                ps.setInt(3, button.index);
                ps.executeUpdate();
                ps.clearParameters();
            }
            ps.close();
        }
    }

    public static List<DBClassTableButton> getButtons(final String id) throws SQLException {
        List<DBClassTableButton> list = new LinkedList<DBClassTableButton>();
        final String command = "SELECT * FROM GDC_CLASSTABLE_BUTTON WHERE CLASSTABLE_ID = ?";
        try (final Connection conn = GDDBData.getConnection();
             final PreparedStatement ps = conn.prepareStatement(command)) {
            ps.setString(1, id);
            try (final ResultSet rs = ps.executeQuery()) {
                list = wrap(rs);
                conn.commit();
            } catch (SQLException ex) {
                throw ex;
            }
        } catch (SQLException ex2) {
            final Object[] args = {id, "GDC_CLASSTABLE_BUTTON"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_TABLE_BY_ID", args);
            GDMsgLogger.addError(msg);
            GDMsgLogger.addError(ex2);
        }
        return list;
    }

    private static List<DBClassTableButton> wrap(final ResultSet rs) throws SQLException {
        final LinkedList<DBClassTableButton> list = new LinkedList<DBClassTableButton>();
        while (rs.next()) {
            final DBClassTableButton ctb = new DBClassTableButton();
            ctb.classTableID = rs.getString(1);
            ctb.buttonID = rs.getString(2);
            ctb.index = rs.getInt(3);
            if (ctb.buttonID != null) {
                ctb.dbButton = DBSkillButton.getByButtonID(ctb.buttonID);
                if (ctb.dbButton != null) {
                    ctb.dbSkill = DBSkill.get(ctb.dbButton.getSkillID());
                }
            }
            list.add(ctb);
        }
        return list;
    }
}
